<?php

namespace backend\controllers;

use backend\actions\ViewAction;
use Yii;
use backend\models\search\PhotoSearch;
use backend\models\Photo;
use backend\actions\CreateAction;
use backend\actions\UpdateAction;
use backend\actions\IndexAction;
use backend\actions\DeleteAction;
use backend\actions\SortAction;
use yii\web\NotFoundHttpException;

/**
 * PhotoController implements the CRUD actions for Photo model.
 */
class PhotoController extends \yii\web\Controller
{
    public function actions()
    {
        return [
            'index'      => [
                'class' => IndexAction::className(),
                'data'  => function () {

                    $searchModel  = new PhotoSearch();
                    $dataProvider = $searchModel->search(yii::$app->getRequest()->getQueryParams());

                    return [
                        'dataProvider' => $dataProvider,
                        'searchModel'  => $searchModel,
                    ];

                }
            ],
            'create'     => [
                'class'      => CreateAction::className(),
                'modelClass' => Photo::className(),
            ],
            'update'     => [
                'class'      => UpdateAction::className(),
                'modelClass' => Photo::className(),
            ],
            'view-layer' => [
                'class'      => ViewAction::className(),
                'modelClass' => Photo::className(),
            ],
            'delete'     => [
                'class'      => DeleteAction::className(),
                'modelClass' => Photo::className(),
            ],
            'sort'       => [
                'class'      => SortAction::className(),
                'modelClass' => Photo::className(),
            ],
        ];
    }

    public function findModel($id)
    {
        if (($model = Photo::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('请求的页面不存在！');
        }
    }

    public function actionIllegal($id)
    {
        $model = $this->findModel($id);
        if ($model->illegal()) {
            return $this->redirect(['index']);
        }
    }


}
